// 获取两经纬度之间的距离
export const getDistance = (lat1, lon1, lat2, lon2) => {
	const R = 6371e3; // 地球半径，单位：米
	const φ1 = lat1 * (Math.PI / 180);
	const φ2 = lat2 * (Math.PI / 180);
	const Δφ = (lat2 - lat1) * (Math.PI / 180);
	const Δλ = (lon2 - lon1) * (Math.PI / 180);

	const a = Math.sin(Δφ / 2) * Math.sin(Δφ / 2) +
		Math.cos(φ1) * Math.cos(φ2) *
		Math.sin(Δλ / 2) * Math.sin(Δλ / 2);
	const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

	return R * c;
}

// 判断是否在中国范围内
export const outOfChina = (lat, lng) => {
	return (lng < 72.004 || lng > 137.8347) || (lat < 0.8293 || lat > 55.8271);
}

// 判断某点是否在某圆形区域内
export const pointInCircle = (circleLat, circleLng, radius, pointLat, pointLng) => {
	let pointToCircle = getDistance(circleLat, circleLng, pointLat, pointLng)
	return pointToCircle < radius

}
// 判断某点是否在对多边形区域内
export const pointInPolygon = (points, pointLat, pointLng) => {
	let inside = false;
	for (let i = 0, j = points.length - 1; i < points.length; j = i++) {
		const xi = points[i].latitude,
			yi = points[i].longitude;
		const xj = points[j].latitude,
			yj = points[j].longitude;

		const intersect = ((yi > pointLng) !== (yj > pointLng)) &&
			(pointLat < (xj - xi) * (pointLng - yi) / (yj - yi) + xi);
		if (intersect) inside = !inside;
	}
	return inside;
}

export const getRectanglePoint = (lat1, lng1, lat2, lng2) => {
	return {
		points: [{
				latitude: Math.max(lat1, lat2),
				longitude: Math.min(lng1, lng2)
			},
			{
				latitude: Math.max(lat1, lat2),
				longitude: Math.max(lng1, lng2)
			},
			{
				latitude: Math.min(lat1, lat2),
				longitude: Math.max(lng1, lng2)
			},
			{
				latitude: Math.min(lat1, lat2),
				longitude: Math.min(lng1, lng2)
			}
		],
		center: {
			latitude: (lat1 + lat2) / 2,
			longitude: (lng1 + lng2) / 2
		}
	}
}

// 获取多边形重心
export const getPolygonCenter = (points) => {
	let totalLng = 0;
	let totalLat = 0;
	const length = points.length;

	for (let i = 0; i < length; i++) {
		totalLng += points[i].longitude;
		totalLat += points[i].latitude;
	}

	return {
		latitude: totalLat / length,
		longitude: totalLng / length
	}
}

// 获取某经纬度正东distance米的经纬度
export const getCircleRangePoints = (centerLat, centerLon, radius) => {
		const R = 6378137; // 地球半径（WGS84 椭球体半长轴）
	    const latRad = centerLat * (Math.PI / 180); // 将纬度转换为弧度
	    const r = R * Math.cos(latRad); // 该纬度圈的半径
	
	    // 计算纬度变化量
	    const deltaLat = radius / R * (180 / Math.PI);
	    // 计算经度变化量
	    const deltaLon = radius / r * (180 / Math.PI);
	
	    // 计算四个方向顶点的经纬度
	    const east = [centerLat, centerLon + deltaLon];
	    const south = [centerLat - deltaLat, centerLon];
	    const west = [centerLat, centerLon - deltaLon];
	    const north = [centerLat + deltaLat, centerLon];
		return {
			East:{
				latitude: centerLat,
				longitude: centerLon + deltaLon
			},
			South:{
				latitude: centerLat - deltaLat,
				longitude: centerLon
			},
			West:{
				latitude: centerLat,
				longitude: centerLon - deltaLon
			},
			North:{
				latitude: centerLat + deltaLat,
				longitude: centerLon
			}
		}
}


export const getRandomInt = (min, max) => {
	min = Math.ceil(min);
	max = Math.floor(max);
	return Math.floor(Math.random() * (max - min + 1)) + min;
}